{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d09f14d-a726-4563-8e2d-c80d21306f6d",
   "metadata": {},
   "source": [
    "Dataset Searching & Querying\n",
    "============================\n",
    "\n",
    "Finding Datasets\n",
    "----------------\n",
    "Individual datasets for a product can be searched for using a datacube instance's `find_datasets` method.\n",
    "\n",
    "For example, we could search for an example dataset from the `ls9_sr` product:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6d9e4ca-aac2-4bdd-97d6-601ee9c43670",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Dataset <id=d853931f-f37d-5ed0-98a9-20753caf97f8 product=ls9_sr location=s3://deafrica-landsat/collection02/level-2/standard/oli-tirs/2022/177/042/LC09_L2SP_177042_20220304_20220306_02_T1/LC09_L2SP_177042_20220304_20220306_02_T1_SR_stac.json>]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datacube\n",
    "\n",
    "dc = datacube.Datacube(app=\"my_analysis\")\n",
    "\n",
    "datasets = dc.find_datasets(product=\"ls9_sr\", limit=1)\n",
    "datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68ea0983-fac6-4a5c-90f9-7270dfc811e9",
   "metadata": {},
   "source": [
    "We can also search for datasets within a specific spatial extent or time period. To do this, we supply a spatiotemporal query (i.e. a range of x- and y-coordinates defining the spatial area to load, and a range of times). \n",
    "\n",
    "`dc.find_datasets()` will then return a subset of datasets that match this query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d9242459-43f7-4212-95b2-681928e9bb8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Dataset <id=8a7ae87d-2032-527f-93af-bb6a59c4f972 product=ls9_sr location=s3://deafrica-landsat/collection02/level-2/standard/oli-tirs/2022/177/043/LC09_L2SP_177043_20220131_20220202_02_T1/LC09_L2SP_177043_20220131_20220202_02_T1_SR_stac.json>,\n",
       " Dataset <id=e83c49c0-a10a-57e4-846b-e07e2ebe1a74 product=ls9_sr location=s3://deafrica-landsat/collection02/level-2/standard/oli-tirs/2022/177/043/LC09_L2SP_177043_20220115_20220118_02_T1/LC09_L2SP_177043_20220115_20220118_02_T1_SR_stac.json>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datasets = dc.find_datasets(\n",
    "    product=\"ls9_sr\",\n",
    "    x=(29.0, 29.01),\n",
    "    y=(25.0, 25.01),\n",
    "    time=(\"2022-01-01\", \"2022-02-01\"),\n",
    ")\n",
    "datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107c436a-49c6-44bc-8c5c-a2b7086f8827",
   "metadata": {},
   "source": [
    "Inspecting Datasets\n",
    "-------------------\n",
    "Dataset objects contain important metadata that are required for loading and interpreting datacube data.\n",
    "These include the dataset's URIs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "da27975b-2bdb-4745-a6cc-52f64336500d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['s3://deafrica-landsat/collection02/level-2/standard/oli-tirs/2022/177/043/LC09_L2SP_177043_20220131_20220202_02_T1/LC09_L2SP_177043_20220131_20220202_02_T1_SR_stac.json']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datasets[0].uris"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a51d3ca3-9d99-4427-9e84-b453f75d9c05",
   "metadata": {},
   "source": [
    "A list of measurements available within the dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4e60ed56-b31d-4232-9d13-57e1b2fe50ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'SR_B1': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B1.TIF'},\n",
       " 'SR_B2': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B2.TIF'},\n",
       " 'SR_B3': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B3.TIF'},\n",
       " 'SR_B4': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B4.TIF'},\n",
       " 'SR_B5': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B5.TIF'},\n",
       " 'SR_B6': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B6.TIF'},\n",
       " 'SR_B7': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_B7.TIF'},\n",
       " 'QA_PIXEL': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_QA_PIXEL.TIF'},\n",
       " 'QA_RADSAT': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_QA_RADSAT.TIF'},\n",
       " 'SR_QA_AEROSOL': {'path': 'LC09_L2SP_177043_20220131_20220202_02_T1_SR_QA_AEROSOL.TIF'}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datasets[0].measurements"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c04cca11-47cc-46fc-afb5-0437a632acc2",
   "metadata": {},
   "source": [
    "The dataset's native coordinate reference system (CRS) and geotransform:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4287719d-9fca-4194-807b-dd3299339981",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CRS('epsg:32635')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datasets[0].crs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "242eb6d2-858a-4e83-84c6-5bb10c76d4ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Affine(226830.0, 0.0, 581385.0,\n",
       "       0.0, -231030.0, 2831715.0)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datasets[0].transform"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dbc703a-524b-4a59-9623-ad8998d797ca",
   "metadata": {},
   "source": [
    "Other important metadata fields that can be used to query and search for data can be accessed using the `metadata` property:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb929ff2-97e9-4896-a45e-805d950ead94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['cloud_cover',\n",
       " 'collection_category',\n",
       " 'creation_dt',\n",
       " 'creation_time',\n",
       " 'crs_raw',\n",
       " 'data_coverage',\n",
       " 'eo_gsd',\n",
       " 'eo_sun_azimuth',\n",
       " 'eo_sun_elevation',\n",
       " 'format',\n",
       " 'grid_spatial',\n",
       " 'id',\n",
       " 'instrument',\n",
       " 'label',\n",
       " 'lat',\n",
       " 'lon',\n",
       " 'measurements',\n",
       " 'platform',\n",
       " 'product_family',\n",
       " 'region_code',\n",
       " 'rmse',\n",
       " 'rmse_x',\n",
       " 'rmse_y',\n",
       " 'sat_orbit_state',\n",
       " 'sat_relative_orbit',\n",
       " 'sources',\n",
       " 'time']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(datasets[0].metadata)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
